<?php

namespace App;

use App\Model;

class Topic extends Model
{
    //多对多关联,属于这个专题的所有文章
    public function posts()
    {   //Topic模型，通过post_topics表，去关联Post模型
        //对应的字段是Topic表的id对应post_topics表的topic_id,Post表对应的是post_topics表的post_id
        return $this->belongsToMany(\App\Post::class, 'post_topics', 'topic_id', 'post_id');
    }

    //专题的文章数,用于withcount
    public function postTopics()
    {
        return $this->hasMany(\App\PostTopic::class,'topic_id','id');
    }
}
